<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode==='show'" ref="dialogForm" label-width="100px" label-position="right">
			<el-form-item label="上级部门" prop="parentId">
				<el-cascader v-model="form.parentId" :options="options" :props="optionsProps" :show-all-levels="false" clearable style="width: 100%;"></el-cascader>
			</el-form-item>
			<el-form-item label="部门名称" prop="deptName">
				<el-input v-model="form.deptName" placeholder="请输入部门名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="排序号" prop="orderNum">
				<el-input-number v-model="form.orderNum" :min="1"></el-input-number>
			</el-form-item>
			<el-form-item label="是否启用" prop="status">
				<el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" clearable type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!=='show'" type="primary" :loading="isSaving" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			// 表单元素控制
			mode: "add",
			titleMap: {
				add: '新增',
				edit: '编辑',
				show: '查看'
			},
			visible: false,
			isSaving: false,
			// 所需数据选项
			options: [],
			optionsProps: {
				value: "id",
				label: "deptName",
				children: "children",
				emitPath: false,
				checkStrictly: true,
				disabled: (node) => {
					// 禁用本身
					return node.id === this.form.id;
				}
			},
			// 表单数据
			form: {
				id: null,
				parentId: null,
				deptName: null,
				orderNum: 1,
				status: "1",
				remark: null
			},
			// 表单验证规则
			rules: {
				deptName: [
					{required: true, message: '请输入部门名称', trigger: "blur"}
				],
				orderNum: [
					{required: true, message: '请输入排序号', trigger: 'change'}
				],
				status: [
					{required: true, message: '请选择是否启用', trigger: 'change'}
				]
			}
		}
	},
	mounted() {
		this.getOptions();
	},
	methods: {
		// 显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		// 表单注入数据
		setData(data) {
			this.form.id = data.id;
			this.form.parentId = data.parentId;
			this.form.deptName = data.deptName;
			this.form.orderNum = data.orderNum;
			this.form.status = data.status;
			this.form.remark = data.remark;
		},
		// 加载树数据
		async getOptions() {
			this.options = await this.$API.manage.dept.list.get();
		},
		// 表单提交
		submit() {
			this.$refs.dialogForm.validate((valid) => {
				if (valid) {
					this.isSaving = true;
					if (this.form.id) {
						this.$API.manage.dept.edit.put(this.form).then(() => {
							this.$emit('success');
							this.visible = false;
							this.$message.success("保存成功");
						}).catch(() => {
							this.isSaving = false;
						});
					} else {
						this.$API.manage.dept.add.post(this.form).then(() => {
							this.$emit('success');
							this.visible = false;
							this.$message.success("保存成功");
						}).catch(() => {
							this.isSaving = false;
						});
					}
				}
			})
		}
	}
}
</script>

<style>
</style>
